﻿using Sustainalytics.ClientDW.Entities;
using Sustainalytics.Entities;
using Sustainalytics.Entities.ProductInvolvement;
using Sustainalytics.Synchronization.SyncFeatureUniverses;
using Sustainalytics.Universes.Synchronization;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Threading;

namespace SyncFeatureUniversesConsole
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var clientUniversesConnectionString = ConfigurationManager.ConnectionStrings["ClientUniverses"].ConnectionString;
            var companyUniverses = new UniverseCompanyHandle(clientUniversesConnectionString).GetAllUniverseCompany();

            var handlers = AssureFeatureUniversesHandlers();
            var spinThread = new Thread(ShowSpinner);
            spinThread.Start();
            
            foreach (var handler in handlers)
            {
                var stopwatch = new Stopwatch();
                stopwatch.Start();
                handler.UpdateUniverseIdsAsync(companyUniverses).Wait();
                Console.WriteLine();

                stopwatch.Stop();

                Console.Write("\rFinish setting universe id's for collection " + handler.CollectionName + " in " + stopwatch.Elapsed +"\n");
                
            }
            Console.Write("\r End. Press any key to close this window.");
            Console.ReadKey();
        }

        private static void ShowSpinner()
        {
            var spin = new ConsoleSpinner();
            while (true)
            {
                spin.Turn();
            }
        }

        private static IEnumerable<IAssureFeatureUniverse> AssureFeatureUniversesHandlers()
        {
            string governanceDWConnectionString = ConfigurationManager.ConnectionStrings["GovenanceDW"].ConnectionString;
            string clientDWConnectionString = ConfigurationManager.ConnectionStrings["ClientDW"].ConnectionString;
            string clientPlatformConnectionString = ConfigurationManager.ConnectionStrings["ClientPlatform"].ConnectionString;
            string newScreeningDataConnectionString = ConfigurationManager.ConnectionStrings["ScreeningDW"].ConnectionString;

            var handlers = new List<IAssureFeatureUniverse>()
            {
                //collection in assure feature universes
                //new AssureFeatureUniverseByIdHandler<CorporateData>(clientDWConnectionString),
                //new AssureFeatureUniverseByIdHandler<FinancialData>(clientDWConnectionString),
                //new AssureFeatureUniverseByIdHandler<CompanyKeyESGIssue>(clientDWConnectionString),
                //new AssureFeatureUniverseByIdHandler<GCOverallCompanySummary>(clientDWConnectionString),
                //new AssureFeatureUniverseByIdHandler<GCOverallCompanySummaryExtended>(clientDWConnectionString),
                //new AssureFeatureUniverseByIdHandler<PICompanyAssessment>(clientDWConnectionString),
                //new AssureFeatureUniverseByCompanyIdHandler<RPControversyIndicator>(clientDWConnectionString),
                //new AssureFeatureUniverseByCompanyIdHandler<EventIndicator>(clientDWConnectionString),
                //new AssureFeatureUniverseByCompanyIdHandler<PICompanyAssessmentShortReport>(clientDWConnectionString),
                //new AssureFeatureUniverseByIdHandler<GovernanceCompanyProfile>(governanceDWConnectionString),
                new AssureFeatureUniverseByCompanyIdHandler<CompanySearch>(clientDWConnectionString),

                //collection not included in assure feature universes synk step
                //new AssureFeatureUniverseByIdHandler<EsgRating>(clientDWConnectionString),
                //new AssureFeatureUniverseByIdHandler<RPIndicator>(clientDWConnectionString),
                //new AssureFeatureUniverseByIdHandler<ESGRpIndicator>(clientDWConnectionString),
                //new AssureFeatureUniverseByIdHandler<CarbonData.BussinesLogic.CarbonData>(clientDWConnectionString),
                //new AssureFeatureUniverseByIdHandler<GovernanceProfile>(clientDWConnectionString),
                //new AssureFeatureUniverseByIdHandler<MostActiveReport>(clientPlatformConnectionString),
                //new AssureFeatureUniverseByCompanyIdHandler<RPCompanyESGComputationResult>(clientPlatformConnectionString),
                //new AssureFeatureUniverseByIdHandler<ScreeningData>(newScreeningDataConnectionString),
                //new AssureFeatureUniverseByCompanyIdHandler<MostRecentActivity>(clientPlatformConnectionString)
            };
            return handlers;
        }
    }

    public class ConsoleSpinner
    {
        private int counter;

        public void Turn()
        {
            counter++;
            switch (counter % 4)
            {
                case 0: Console.Write("/"); counter = 0; break;
                case 1: Console.Write("-"); break;
                case 2: Console.Write("\\"); break;
                case 3: Console.Write("|"); break;
            }
            Thread.Sleep(100);
            Console.SetCursorPosition(0, Console.CursorTop);
        }
    }
}
